package core.semantic;

import java.util.HashSet;

import core.semantic.SemanticTuple.Class;

/**
 * It represents the Symbol Table of the compiler.
 */
public class SymbolTable {

	private HashSet<SemanticTuple> table;
	
	/**
	 * Constructs a new <code>SymbolTable</code>.
	 */
	public SymbolTable() {
		table = new HashSet<SemanticTuple>();
	}
	
	/**
	 * It adds the semantic tuple provided to this table.
	 * @param e The semantic tuple to be added.
	 * @return <code>true</code> if the semantic tuple had been added suscesfully.
	 */
	public boolean add(SemanticTuple e) {
		if(find(e.getIdentifier(), e.getIdClass()) != null){
			return false;
		}
		
		return table.add(e);
	}
	
	/**
	 * It finds the semantic tuple that represents the variable that has the
	 * identifier and class provided.
	 * @param identifier The identifier of the variable to be find.
	 * @param c The class of the variable to be find.
	 * @return The semantic tuple that represents the variable that has the
	 * identifier and class provided, if there isn�t the tuple, it will 
	 * return <code>null</code>.
	 */
	public SemanticTuple find(String identifier, Class c){
		for (SemanticTuple st : table) {
			if(st.getIdentifier().equals(identifier) && st.getIdClass().equals(c)){
				return st;
			}
		}
		return null;
	}
}
